//
//  RCActivityDiscoutCell.m
//  RocketProject
//
//  Created by Wu Chengwei on 13-9-27.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCActivityDiscoutCell.h"
#import "UIColor-Expanded.h"
#import "CommonDefines.h"
#import "RCActivityDiscountListDataModel.h"
#import "UIImageView+WebCache.h"
#import "NSString+NSString_Ex_.h"

#define kDefaultimageFile        @"image_recommendFood_image_bg.png"
#define kLineImageFile           @"image_line_type3.png"
#define kBGImageFile             @"image_discount_cell_bg.png"
#define kBaomingImageFile        @"image_activityDiscount_baoming.png"

@interface RCActivityDiscoutCell()

@property(nonatomic,retain)UIImageView *defaultImageView;
@property(nonatomic,retain)UIImageView *picImageView;
@property(nonatomic,retain)UILabel     *activityNameLabel;
@property(nonatomic,retain)UILabel     *activityTimeLabel;
@property(nonatomic,retain)UILabel     *renqiWordLabel;
@property(nonatomic,retain)UILabel     *renqiLabel;
@property(nonatomic,retain)UILabel     *baomingWordLabel;
@property(nonatomic,retain)UILabel     *baomingLabel;
@property(nonatomic,retain)UILabel     *formerLabel;
@property(nonatomic,retain)UIView      *formerLineView;
@property(nonatomic,retain)UILabel     *presentPriceWordLabel;
@property(nonatomic,retain)UILabel     *presentPriceLabel;
@property(nonatomic,retain)UIImageView *baomingImageView;

@end

@implementation RCActivityDiscoutCell

@synthesize defaultImageView = _defaultImageView;
@synthesize picImageView = _picImageView;
@synthesize activityNameLabel = _activityNameLabel;
@synthesize activityTimeLabel = _activityTimeLabel;
@synthesize renqiWordLabel = _renqiWordLabel;
@synthesize renqiLabel = _renqiLabel;
@synthesize baomingWordLabel = _baomingWordLabel;
@synthesize baomingLabel = _baomingLabel;
@synthesize formerLabel = _formerLabel;
@synthesize formerLineView = _formerLineView;
@synthesize presentPriceWordLabel = _presentPriceWordLabel;
@synthesize presentPriceLabel = _presentPriceLabel;
@synthesize baomingImageView = _baomingImageView;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        UIImage *bgImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kBGImageFile ofType:nil]];
        [self.contentView setBackgroundColorWithImage:bgImage];
        [self createPlaceHolderView];
    }
    return self;
}

-(void)dealloc{
    self.defaultImageView = nil;
    self.picImageView = nil;
    self.activityNameLabel = nil;
    self.activityTimeLabel = nil;
    self.renqiWordLabel = nil;
    self.renqiLabel = nil;
    self.baomingWordLabel = nil;
    self.baomingLabel = nil;
    self.formerLabel = nil;
    self.formerLineView = nil;
    self.presentPriceWordLabel = nil;
    self.presentPriceLabel = nil;
    self.baomingImageView = nil;
    [super dealloc];
}

-(void)createPlaceHolderView{
    _defaultImageView = [[UIImageView alloc]initWithFrame:CGRectMake(14, 14, 84, 66)];
    _defaultImageView.backgroundColor = [UIColor clearColor];
    UIImage *defaultImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kDefaultimageFile ofType:nil]];
    defaultImage = [defaultImage resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
    [_defaultImageView setImage:defaultImage];
    [self addSubview:_defaultImageView];
    
    _picImageView = [[UIImageView alloc]initWithFrame:CGRectMake(17, 17, 78, 60)];
    _picImageView.backgroundColor = [UIColor clearColor];
    [self addSubview:_picImageView];
    
    _activityNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(108, 19, 202, 16)];
    _activityNameLabel.backgroundColor = [UIColor clearColor];
    _activityNameLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:16];
    _activityNameLabel.textColor = [UIColor colorWithHex:0xff505050];
    [self addSubview:_activityNameLabel];
    
    _activityTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(108, 43, 202, 12)];
    _activityTimeLabel.backgroundColor = [UIColor clearColor];
    _activityTimeLabel.textColor = [UIColor colorWithHex:0xff979797];
    _activityTimeLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:12];
    [self addSubview:_activityTimeLabel];
    
    _renqiWordLabel = [[UILabel alloc]initWithFrame:CGRectMake(108, 60, 100, 12)];
    _renqiWordLabel.backgroundColor = [UIColor clearColor];
    _renqiWordLabel.textColor = [UIColor colorWithHex:0xff979797];
    _renqiWordLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:12];
    _renqiWordLabel.text = @"人气：";
    CGSize renqiWordSize = [_renqiWordLabel.text sizeWithFont:_renqiWordLabel.font constrainedToSize:CGSizeMake(200, 20)];
    CGRect renqiWordRect = _renqiWordLabel.frame;
    renqiWordRect.size.width = renqiWordSize.width;
    _renqiWordLabel.frame = renqiWordRect;
    [self addSubview:_renqiWordLabel];
    
    _renqiLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_renqiWordLabel.frame), 60, 100, 12)];
    _renqiLabel.backgroundColor = [UIColor clearColor];
    _renqiLabel.textColor = [UIColor colorWithHex:0xffff8200];
    _renqiLabel.font = [UIFont fontWithName:kFontNameHelvetica size:12];
    [self addSubview:_renqiLabel];
    
    _baomingWordLabel = [[UILabel alloc]initWithFrame:CGRectMake(200, 60, 100, 12)];
    _baomingWordLabel.backgroundColor = [UIColor clearColor];
    _baomingWordLabel.textColor = [UIColor colorWithHex:0xff979797];
    _baomingWordLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:12];
    _baomingWordLabel.text = @"报名：";
    CGSize baomingWordSize = [_baomingWordLabel.text sizeWithFont:_baomingWordLabel.font constrainedToSize:CGSizeMake(200, 20)];
    CGRect baomingWordRect = _baomingWordLabel.frame;
    baomingWordRect.size.width = baomingWordSize.width;
    _baomingWordLabel.frame = baomingWordRect;
    [self addSubview:_baomingWordLabel];
    
    _baomingLabel = [[UILabel alloc]initWithFrame:CGRectMake(250, 60, 100, 12)];
    _baomingLabel.backgroundColor = [UIColor clearColor];
    _baomingLabel.textColor = [UIColor colorWithHex:0xffff8200];
    _baomingLabel.font = [UIFont fontWithName:kFontNameHelvetica size:12];
    [self addSubview:_baomingLabel];
    
    _formerLabel = [[UILabel alloc]initWithFrame:CGRectMake(108, 77, 100, 12)];
    _formerLabel.backgroundColor = [UIColor clearColor];
    _formerLabel.textColor = [UIColor colorWithHex:0xff979797];
    _formerLabel.font = [UIFont fontWithName:kFontNameHelvetica size:12];
    [self addSubview:_formerLabel];
    
    _formerLineView = [[UIView alloc]initWithFrame:CGRectMake(145, 82, 50, 1)];
    _formerLineView.backgroundColor = [UIColor colorWithHex:0xff979797];
    [self addSubview:_formerLineView];
    
    _presentPriceWordLabel = [[UILabel alloc]initWithFrame:CGRectMake(200, 77, 30, 12)];
    _presentPriceWordLabel.backgroundColor = [UIColor clearColor];
    _presentPriceWordLabel.textColor = [UIColor colorWithHex:0xff979797];
    _presentPriceWordLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:12];
    _presentPriceWordLabel.text = @"活动价：￥";
    CGSize presentWordSize = [_presentPriceWordLabel.text sizeWithFont:_presentPriceWordLabel.font constrainedToSize:CGSizeMake(200, 20)];
    CGRect presentWordRect = _presentPriceWordLabel.frame;
    presentWordRect.size.width = presentWordSize.width;
    _presentPriceWordLabel.frame = presentWordRect;
    [self addSubview:_presentPriceWordLabel];
    
    _presentPriceLabel = [[UILabel alloc]initWithFrame:CGRectMake(250, 77, 100, 12)];
    _presentPriceLabel.backgroundColor = [UIColor clearColor];
    _presentPriceLabel.textColor = [UIColor colorWithHex:0xffff8200];
    _presentPriceLabel.font = [UIFont fontWithName:kFontNameHelvetica size:12];
    [self addSubview:_presentPriceLabel];
    
    _baomingImageView = [[UIImageView alloc]initWithFrame:CGRectMake(250, 69, 41, 20)];
    _baomingImageView.backgroundColor = [UIColor clearColor];
    [_baomingImageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kBaomingImageFile ofType:Nil]]];
    [self addSubview:_baomingImageView];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)bindData:(RCActivityDiscountListItem *)dataModel{
    if (nil == dataModel) {
        return;
    }
    if (![NSString stringIsNullOrEmpty:dataModel.activityPicUrl]) {
        [_picImageView setImageWithURL:[NSURL URLWithString:dataModel.activityPicUrl] placeholderImage:nil];
    }
    
    _activityNameLabel.hidden = YES;
    if (![NSString stringIsNullOrEmpty:dataModel.activityName]) {
        _activityNameLabel.hidden = NO;
        _activityNameLabel.text = dataModel.activityName;
    }
    
    _activityTimeLabel.hidden = YES;
    if (![NSString stringIsNullOrEmpty:dataModel.activityTime]) {
        _activityTimeLabel.hidden = NO;
        _activityTimeLabel.text = dataModel.activityTime;
    }
    
    _renqiWordLabel.hidden = YES;
    _renqiLabel.hidden = YES;
    if (dataModel.activityHots > 0) {
        _renqiWordLabel.hidden = NO;
        _renqiLabel.hidden = NO;
        _renqiLabel.text = [NSString stringWithFormat:@"%d",dataModel.activityHots];
        CGSize size = [_renqiLabel.text sizeWithFont:_renqiLabel.font constrainedToSize:CGSizeMake(200, 20)];
        CGRect rect = _renqiLabel.frame;
        rect.size.width = size.width;
        _renqiLabel.frame = rect;
    }
    
    _baomingWordLabel.hidden = YES;
    _baomingLabel.hidden = YES;
    if (dataModel.activitySignup > 0) {
        _baomingWordLabel.hidden = NO;
        _baomingLabel.hidden = NO;
        
        CGRect wordRect = _baomingWordLabel.frame;
        wordRect.origin.x = CGRectGetMaxX(_renqiLabel.frame)+10;
        _baomingWordLabel.frame = wordRect;
        
        _baomingLabel.text = [NSString stringWithFormat:@"%d",dataModel.activitySignup];
        CGSize size = [_baomingLabel.text sizeWithFont:_baomingLabel.font constrainedToSize:CGSizeMake(200, 20)];
        CGRect rect = _baomingLabel.frame;
        rect.size.width = size.width;
        rect.origin.x = CGRectGetMaxX(_baomingWordLabel.frame);
        _baomingLabel.frame = rect;
    }
    
    _formerLabel.hidden = YES;
    _formerLineView.hidden = YES;
    if (dataModel.activityOriginalPrice > 0) {
        _formerLabel.hidden = NO;
        _formerLineView.hidden = NO;
        _formerLabel.text = [NSString stringWithFormat:@"原价：￥%d",dataModel.activityOriginalPrice];
        CGSize size = [_formerLabel.text sizeWithFont:_formerLabel.font constrainedToSize:CGSizeMake(200, 20)];
        CGRect rect = _formerLabel.frame;
        rect.size.width = size.width;
        _formerLabel.frame = rect;
        
        CGRect lineRect = _formerLineView.frame;
        lineRect.size.width = CGRectGetMaxX(_formerLabel.frame)-lineRect.origin.x+3;
        _formerLineView.frame = lineRect;
    }
    
    _presentPriceWordLabel.hidden = YES;
    _presentPriceLabel.hidden = YES;
    if (dataModel.activityPresentPrice > 0) {
        _presentPriceWordLabel.hidden = NO;
        _presentPriceLabel.hidden = NO;
        
        CGRect wordRect = _presentPriceWordLabel.frame;
        wordRect.origin.x = CGRectGetMaxX(_formerLabel.frame)+10;
        _presentPriceWordLabel.frame = wordRect;
        
        _presentPriceLabel.text = [NSString stringWithFormat:@"%d",dataModel.activityPresentPrice];
        CGSize size = [_presentPriceLabel.text sizeWithFont:_presentPriceLabel.font constrainedToSize:CGSizeMake(200, 20)];
        CGRect rect = _presentPriceLabel.frame;
        rect.size.width = size.width;
        rect.origin.x = CGRectGetMaxX(_presentPriceWordLabel.frame);
        _presentPriceLabel.frame = rect;
    }
    
    CGRect rect = _baomingImageView.frame;
    rect.origin.x = CGRectGetMaxX(_presentPriceLabel.frame)+5;
    _baomingImageView.frame = rect;
}

@end
